//
//  NewFeatureViewController.swift
//  weibo1
//
//  Created by Apple on 15/11/29.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SnapKit
private let reuseIdentifier = "Cell"
private let maxitem = 4
class NewFeatureViewController: UICollectionViewController {
    
    init(){
        //设置布局方式
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = .Horizontal
        //        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        super.init(collectionViewLayout: layout)
        //设置分页效果
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //注册cell
        self.collectionView!.registerClass(NewFeaturecell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        // Do any additional setup after loading the view.
    }
    
    
    
    
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       
        return maxitem
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeaturecell
        cell.index = indexPath.item
        

        
        return cell
    }
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
//        print(indexPath.item)
        let cell = collectionView.visibleCells().last as! NewFeaturecell
        let index = collectionView.indexPathForCell(cell)
        let indexitem = index?.item ?? 0
        if indexitem == maxitem - 1{
            cell.startAnimation()
            
        }
        
    }
    
    
}

//MARK: 自定义cell
class NewFeaturecell: UICollectionViewCell{
    //定义一个属性来改变图片的角标
    var index: Int = 0{
        didSet{
        
        iconView.image = UIImage(named: "new_feature_\(index + 1)")
            //隐藏按钮
        startBut.hidden = index != 3
           
        }
    }
    
    
    //重写初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
    
        contentView.addSubview(iconView)
        contentView.addSubview(startBut)
        //设置子控件的约束
        iconView.snp_makeConstraints { (make) -> Void in
            //相对于约束的对象
            make.edges.equalTo(contentView.snp_edges)
        }
        startBut.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.bottom.equalTo(iconView.snp_bottom).offset(-180)
            
            //开始体验的按钮事件
            startBut.addTarget(self, action: "startButClick", forControlEvents: .TouchUpInside)
            
        }
    }
    
  
    
 //MAKR:开始体验按钮的点击事件
  @objc private func startButClick(){
        print("我呗点击了")
    //接收通知
    NSNotificationCenter.defaultCenter().postNotificationName(rootViewController, object: "wel")
    
    
    
    }
    //添加开始按钮的动画
    func startAnimation(){
    
        startBut.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            
            self.startBut.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
//                print("OK")
        }

    
    }
    
    
    //懒加载子控件
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    private lazy var startBut: UIButton = {
        
        let but = UIButton()
        but.setTitle("开始体验", forState: .Normal)
        but.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        but.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        
        but.sizeToFit()
        return but
        }()
}
